using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using way.model;
using way.util;

namespace way.model
{
    public class AppDbContext : DbContext
    {
        MenuCore menuCore = InstanceManage.getMenuCore();
        public string tableName;
        private string[] tables = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("appsetting.json", optional: false, reloadOnChange: true)
                .Build()["MenuTables:allTable"].Split(',');
        public DbSet<MenuEntity> menus { get; set; }
        public DbSet<MenuInCoreEntity> inCores { get; set; }
        public AppDbContext(DbContextOptions<AppDbContext> options, string tableName) : base(options)
        {
            this.tableName = tableName;
        }
        //protected override void OnConfiguring(DbContextOptionsBuilder dbContextOptionsBuilder)
        //{
        //    //string connectionString = "server=localhost;port=3306;database=test;user=root;password=admin;charset=utf8mb4;";
        //    string connectionString = coreUtil.getConfigString("DefaultConnection");
        //    dbContextOptionsBuilder.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 11)));
        //}
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            string[] arr = this.tableName.Split(menuCore.split);
            string tableName = string.Empty;
            if (arr.Length==1) {
                tableName = this.tableName;
            }
            if (arr.Length == 2)
            {
                tableName = arr[1];
            }
            string entity = arr[0];
            if (entity==menuCore.inCore)
            {
                modelBuilder.Entity<MenuInCoreEntity>(
                    menu =>
                    {
                        menu.ToTable(tableName);
                        menu.HasKey(m => m.id);
                        menu.Property(m => m.id).ValueGeneratedOnAdd();
                        menu.Property(m => m.context).HasColumnType("text");
                        //menu.HasOne(m => m.Parent).WithMany(m => m.Children).HasForeignKey(m => m.up).OnDelete(DeleteBehavior.Restrict);
                        menu.Property(m => m.create).HasColumnType("datetime");
                        menu.Property(m => m.update).HasColumnType("datetime");
                        //
                        menu.Ignore(m => m.table);
                    }
                );
            }
            else {
                modelBuilder.Entity<MenuEntity>(
                     menu =>
                     {
                         menu.ToTable(tableName);
                         menu.HasKey(m => m.id);
                         menu.Property(m => m.id).ValueGeneratedOnAdd();
                         //menu.HasOne(m => m.Parent).WithMany(m => m.Children).HasForeignKey(m => m.up).OnDelete(DeleteBehavior.Restrict);
                         menu.Property(m => m.create).HasColumnType("datetime");
                         menu.Property(m => m.update).HasColumnType("datetime");
                         //
                         menu.Ignore(m => m.table);
                     }
                 );
            }
            //base.OnModelCreating(modelBuilder);
        }
    }
}
